from pathlib import Path
import json
from datetime import datetime


class Notification:
    def __init__(self, id: int, title: str, content: str, publish_date: str, url: str = "", is_read: bool = False, campus_network_only: bool = False):
        self.id = id
        self.title = title
        self.content = content
        self.publish_date = publish_date
        self.url = url
        self.is_read = is_read
        self.campus_network_only = campus_network_only

    def to_dict(self) -> dict:
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'publish_date': self.publish_date,
            'url': self.url,
            'is_read': self.is_read,
            'campus_network_only': self.campus_network_only
        }

    @classmethod
    def from_dict(cls, data: dict) -> 'Notification':
        """从字典创建Notification对象"""
        return cls(
            id=data['id'],
            title=data['title'],
            content=data['content'],
            publish_date=data['date'],
            url=data.get('url', ""),
            is_read=data.get('is_read', False),
            campus_network_only=data.get('campus_network_only', False)
        )

    @staticmethod
    def load_from_json(file_path: str) -> list['Notification']:
        """从JSON文件加载所有通知信息"""
        path = Path(file_path)
        if not path.exists():
            return []
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        return [Notification.from_dict(item) for item in data]
